/**
 * 设置 弹幕DOM池 每一个通道最多10条弹幕
 **/

const MAX_DM_COUNT = 10;
const CHANNEL_COUNT = 30;

let domPool = [];
let danmuPool = [];
let hasPosition = [];

/**
 * 做一下初始化工作
 */
var sendMessage ={
    init:function () {
        let wrapper = document.getElementById('messageWrapper')
        // 先new一些span 重复利用这些DOM
        for (let j = 0; j < CHANNEL_COUNT; j++) {
            let doms = [];
            for (let i = 0; i < MAX_DM_COUNT; i++) {
                let dom = document.createElement('span');
                wrapper.appendChild(dom);
                dom.className = 'right';
                dom.style.transform =`translateX(${$(wrapper).outerWidth(true)-50}px)`;
                //
                dom.style.top = j * 25 + 'px';
                doms.push(dom);
                dom.addEventListener('transitionend', () => {
                    dom.className = 'right';
                    dom.style.transform =`translateX(${$(wrapper).outerWidth(true)-50 }px)`;
                    domPool[j].push(dom);
                });
            }
            domPool.push(doms);
        }
        for (let i = 0; i < CHANNEL_COUNT; i++) {
            hasPosition[i] = true;
        }
    },
    /**
     * 根据DOM和弹幕信息 发射弹幕
     */
    shootDanmu:function(dom, text, channel) {
    dom.innerText = text;
    dom.style.transform = `translateX(${-dom.clientWidth}px)`;
    dom.className = 'left';
    $(dom).css('color',sendMessage.getReandomColor());
    hasPosition[channel] = false;
    setTimeout(() => {
        hasPosition[channel] = true;
    }, dom.clientWidth * 10 + 1000);
},
    getChannel:function () {
        for (let i = 0; i < CHANNEL_COUNT; i++) {
            if (hasPosition[i] && domPool[i].length) return i;
        }
        return -1;
    },
    //随机获取颜色值
    getReandomColor:function(){
      var color =['rgb(70, 202, 82)','rgb(17, 36, 215)','rgb(17, 215, 202)','rgb(197, 215, 17)','#00BCD4','#ffffff','#9C27B0','rgb(255, 242, 1)'];
      var num = Math.floor((color.length-1)*Math.random());
      return color[num];

},

    stop:function () {
        $('#messageWrapper').html('');
    }
}

